﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class UserControls_SiteVersionTab : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            tabs.AppendDataBoundItems = true;
            tabs.Tabs.Add(new Telerik.Web.UI.RadTab("Pending", null));
            BasePage acmsBasePage = this.Page as BasePage;
            tabs.DataSource = Acms.Bll.SiteVersion.GetAllFuture(acmsBasePage.AcmsContext.SiteId);
            tabs.DataBind();
            tabs.Tabs.Add(new Telerik.Web.UI.RadTab("Live", DateTime.Now.ToString()));
            foreach(RadTab tab in tabs.Tabs)
            {
                if (string.IsNullOrEmpty(tab.Value) && SiteVersion == null)
                {
                    tab.Selected = true;
                }
                else if (SiteVersion != null && tab.ToString() == SiteVersion.Value.ToString())
                {
                    tab.Selected = true;
                }
            }
        }

    }

    public DateTime? SiteVersion { get; set; }
    protected void tabs_TabClick(object sender, RadTabStripEventArgs e)
    {
        DateTime? tmp = null;
        if(!string.IsNullOrEmpty(e.Tab.Value))
        {
            try
            {
                tmp = DateTime.Parse(e.Tab.Value);
            }
            catch{
            }
        }
        if (SiteVersion != tmp)
        {
            SiteVersion = tmp;
            OnSelectedVersionChanged(EventArgs.Empty);
        }
    }

    protected virtual void OnSelectedVersionChanged(EventArgs eventArgs)
    {
        if (SelectedVersionChanged != null)
        {
            SelectedVersionChanged(this, eventArgs);
        }
    }

    public event EventHandler SelectedVersionChanged;
}
